﻿<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>悦宝园管理系统</title>
	<meta name="renderer" content="webkit">	
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">	
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">	
	<meta name="apple-mobile-web-app-status-bar-style" content="black">	
	<meta name="apple-mobile-web-app-capable" content="yes">	
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="css/reset.css">
	<link rel="stylesheet" href="css/main.css">
	<link rel="stylesheet" href="css/index.css">
</head>
<body>
	<div class="content-head">悦宝园会员管理系统</div>
	<div class="content-main">
		<div class="side-nav" ms-controller="navBar">
			<ul class="nav-item">
				<li><a href="main.html" class="nav-item-front nav-item-add">首页</a></li>
				<li ms-for="el in @navTitleArr">
					<a href="javascript:;" class="nav-item-front">{{el.par}}</a>
					<dl class="nav-item-child">
						<dd ms-for="($index, ex) in el.child"><a ms-attr='{href: el.href[$index]}'>{{ex}}</a></dd>
					</dl>
				</li>
			</ul>
		</div>
		<div class="content" ms-controller="main">
			<div class="tab-content">
				<div class="tab-item">
					<div class="main">
						<div class="tips-block">
							<div class="color-block">
								<div class="color-block-left color-block-blue"><b>生日</b></div>
								<div class="color-block-right"><a class="color-block-blue-a" href="birthWarning.html">{{@birth()}}</a></div>
							</div>
							<div class="color-block">
								<div class="color-block-left color-block-orange"><b>升班</b></div>
								<div class="color-block-right"><a class="color-block-orange-a" href="upClass.html">{{@upClass()}}</a></div>
							</div>
							<div class="color-block">
								<div class="color-block-left color-block-green"><b>7天未上课</b></div>
								<div class="color-block-right"><a class="color-block-green-a" href="longTime.html" ms-html="@longTime()">10</a></div>
							</div>
							<div class="color-block">
								<div class="color-block-left color-block-purple"><b>毕业</b></div>
								<div class="color-block-right"><a class="color-block-purple-a" href="offClass.html">{{@offClass()}}</a></div>
							</div>
							<div class="color-block">
								<div class="color-block-left color-block-red"><b>剩余课时数</b></div>
								<div class="color-block-right"><a class="color-block-red-a" href="subClass.html" ms-html="@subClass()">0</a></div>
							</div>
							<div class="color-block">
								<div class="color-block-left color-block-asph"><b>合同到期</b></div>
								<div class="color-block-right"><a class="color-block-asph-a" href="offContract.html">10</a></div>
							</div>
						</div>
						<input type="button" value="结课转移" ms-click="@transMember()">
						<!-- <div class="text-block">
							<div>
								<span>目标消课</span>
								<span>已消课</span>
							</div>
						</div> -->
					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- <div id="outputExcel">下载</div> -->
<!-- <input type="button" value="删除" id="btn"/> -->
<script src="js/jq.js"></script>
<script src="js/avalon.js"></script>
<script src="js/main.js"></script> 
<script src="js/xlsx.full.min.js"></script>
<script>
// $("#btn").click(function() {
// baseFunc.deleteMemberStorageData({tel: "15022982118"});
// });

var allData = baseFunc.getStorageData("member");

var vm = avalon.define({
	$id: "main",
	defaults: {
		onReady: function() {
			alert("asd");
		}
	},
	transMember: function() {
		var tempArr = [];
		for (var i = 0; i < allData.length; i++) {
			if(allData[i].classNum.formal <= 0 && allData[i].classNum.free <= 0) {
				tempArr.push(allData[i]);
				allData.splice(i, 1);
			}
		}

		tempArr;
		localStorage["overMember"] = JSON.stringify(tempArr);
		localStorage["member"] = JSON.stringify(allData);
	},
	birth: function(){
		var now = new Date();
		var resArr = 0;
		var birthArr = [];
		var time = now.getFullYear() + "-" +((now.getMonth()+1)<10?"0":"")+(now.getMonth()+1)+"-"+(now.getDate()<10?"0":"")+now.getDate();

		birthArr = time.split("-");

		for(var i = 0; i < allData.length; i++) {
			var birthday = allData[i].birthday.split("-");
			var flagDate = [now.getFullYear(),birthday[1], birthday[2]].join("-");
			var diffDate = baseFunc.dateDiff(flagDate, time);
			if(diffDate <= 0 && diffDate > -5) {
				resArr++;
			}
		}
		return resArr;
	},
	offClass: function() {
		var ids = 0;
		for (var i = 0; i < allData.length; i++) {
			var formal = parseInt(allData[i].classNum.formal),
				free = parseInt(allData[i].classNum.free);

			if(formal == 0 && free == 0) {
				ids++;
			}
		};
		return ids;
	},
	upClass: function() {
		var newObj = 0;

		var diffDate = function(newDate, oldDate) { //{[], []}
    		var year = newDate[0]; //表示需要查找的年份
			var month = newDate[1];//表示需要查找的月份
			var curMonthDays = new Date(year,month,0).getDate();//0表示3月的第0天，上月的最后一天,月份从0开始记数
			if(oldDate[2] > curMonthDays) {
				newDate[2] = curMonthDays;
			}

    		return newDate[0] + "-" + newDate[1] + "-" + newDate[2];
    	};

    	var calcDate = function(arr, calcNum) {
    		var oldY = parseInt(arr[0]),
    			oldM = parseInt(arr[1]),
    			oldD = parseInt(arr[2]),
    			y = oldY,
    			m = oldM,
    			d = oldD,
    			calcNum = parseInt(calcNum);

    		if(calcNum == 12) {
    			y += 1;
    			return diffDate([y, m, d], [oldY, oldM, oldD]);
    		}

    		if(calcNum == 24) {
    			y += 2;
    			return diffDate([y, m, d], [oldY, oldM, oldD]);
    		}

    		if(calcNum < 12) {
    			m = m + calcNum;
    			if(m > 12) {
    				y += 1;
    				m -= 12;
    			}
    			return diffDate([y, m, d], [oldY, oldM, oldD]);
    		}

    		if(calcNum > 12) {
    			y += 1;
    			calcNum -= 12;
    			m += calcNum;
    			if(m > 12) {
    				y += 1;
    				m -= 12;
    			}
    			return diffDate([y, m, d], [oldY, oldM, oldD]);
    		}
    	};

    	var inWhichTime = function(tipsDate, confirmDate) {
    		//情况一刚好处于提醒日
    		var day2 = new Date();
    		day2.setTime(day2.getTime());
    		var nowDate = day2.getFullYear() + "-" + (day2.getMonth()+1) + "-" + day2.getDate();
    		var effDiff = baseFunc.dateDiff(tipsDate, confirmDate);
    		var tipsDiff = baseFunc.dateDiff(tipsDate, nowDate);
    		var confirmSplit = confirmDate.split("-");

	    	if(tipsDiff >= 0 && tipsDiff < effDiff) {
	    		return "inTips";
	    	}

    		if(day2.getFullYear() == confirmSplit[0] && (day2.getMonth()+1) == confirmSplit[1] && day2.getDate() == confirmSplit[2]) {
    			return "inConfirm";
    		}
    		return "free";
    	};

		var newStatus,
			oldStatus;
		$.each(allData, function(index, el) {
			iContinue = true;
			$.each(el.upClassInfo, function(index, bl) {
				oldStatus = bl[2];
				if(oldStatus == "inTips") {
					switch(index) {
						case "bb": 
							newStatus = inWhichTime(calcDate(el.birthday.split("-"), 5), calcDate(el.birthday.split("-"), 6));
							break;
						case "tod": 
							newStatus = inWhichTime(calcDate(el.birthday.split("-"), 9), calcDate(el.birthday.split("-"), 10));
							break;
						case "run": 
							newStatus = inWhichTime(calcDate(el.birthday.split("-"), 15), calcDate(el.birthday.split("-"), 16));
							break;
						case "twos": 
							newStatus = inWhichTime(calcDate(el.birthday.split("-"), 23), calcDate(el.birthday.split("-"), 24));
							break;
						case "m1": 
							newStatus = inWhichTime(calcDate(el.birthday.split("-"), 9), calcDate(el.birthday.split("-"), 10));
							break;
						case "m2": 
							newStatus = inWhichTime(calcDate(el.birthday.split("-"), 15), calcDate(el.birthday.split("-"), 16));
							break;
						case "m3": 
							newStatus = inWhichTime(calcDate(el.birthday.split("-"), 23), calcDate(el.birthday.split("-"), 24));
							break;
						case "ae1": 
							newStatus = inWhichTime(calcDate(el.birthday.split("-"), 15), calcDate(el.birthday.split("-"), 16));
							break;
						case "ae2": 
							newStatus = inWhichTime(calcDate(el.birthday.split("-"), 23), calcDate(el.birthday.split("-"), 24));
							break;
					}
					bl[2] = newStatus;
				}
				
				if(oldStatus == "inConfirm" || oldStatus == "inTips") {
					if(iContinue) {
						newObj++;
					}
					iContinue = false;
				}
			})
			baseFunc.editStorageData("member", el);
		});
		return newObj;
	},
	subClass: function() {
		var ids = 0;
		for (var i = 0; i < allData.length; i++) {
			var formal = parseInt(allData[i].classNum.formal),
				free = parseInt(allData[i].classNum.free);

			if(formal + free < 10 && (formal != 0 || free != 0)) {
				ids++;
			}
		};
		return ids;
	},
	longTime: function() {
		var ids = 0;
		for (var i = 0; i < allData.length; i++) {
			if (allData[i].passClass) {
				var latestClass = allData[i].passClass[allData[i].passClass.length - 1],
					latestClassTime = latestClass.date,
					day2 = new Date();
	    		day2.setTime(day2.getTime());
	    		var nowDate = day2.getFullYear() + "-" + (day2.getMonth()+1) + "-" + day2.getDate(),
					diffDay = baseFunc.dateDiff(latestClassTime, nowDate);
				if (diffDay > 7) {
					ids++;
				};
			};
		}
		return ids;
	}
});
/*********************** 下载会员表 **************************/
function sheet2blob(sheet, sheetName) {
    sheetName = sheetName || 'sheet1';
    var workbook = {
        SheetNames: [sheetName],
        Sheets: {}
    };
    workbook.Sheets[sheetName] = sheet;
    // 生成excel的配置项
    var wopts = {
        bookType: 'xlsx', // 要生成的文件类型
        bookSST: false, // 是否生成Shared String Table，官方解释是，如果开启生成速度会下降，但在低版本IOS设备上有更好的兼容性
        type: 'binary'
    };
    var wbout = XLSX.write(workbook, wopts);
    var blob = new Blob([s2ab(wbout)], {type:"application/octet-stream"});
    // 字符串转ArrayBuffer
    function s2ab(s) {
        var buf = new ArrayBuffer(s.length);
        var view = new Uint8Array(buf);
        for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    }
    return blob;
}

function openDownloadDialog(url, saveName) {
    if(typeof url == 'object' && url instanceof Blob)
    {
        url = URL.createObjectURL(url); // 创建blob地址
    }
    var aLink = document.createElement('a');
    aLink.href = url;
    aLink.download = saveName || ''; // HTML5新增的属性，指定保存文件名，可以不要后缀，注意，file:///模式下不会生效
    var event;
    if(window.MouseEvent) event = new MouseEvent('click');
    else
    {
        event = document.createEvent('MouseEvents');
        event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    }
    aLink.dispatchEvent(event);
}

function rowspanFun(data, nameList) {
	for (var i = 0; i < nameList.length; i++) {
		var name = nameList[i];
		var startRow = 0;
		var endRow = data.length;
		var mergeNum = 1;
		if (endRow != 1) {
			for (var j = startRow; j < endRow; j++) {
				if (j == endRow - 1) { //判断是否是最后一个元素
					if (startRow == endRow - 1) {
						data[j][name + 'Rowspan'] = 1;
					}
				} else {
					if (data[startRow][name] == data[j + 1][name]) {
						data[j + 1][name + 'Rowspan'] = 0;
						mergeNum = mergeNum + 1;
						data[startRow][name + 'Rowspan'] =mergeNum;
					} else {
						startRow = j + 1;
						if (mergeNum > 1) {
							data[startRow][name + 'Rowspan'] = 1;
						} else {
							data[j][name + 'Rowspan'] = 1;
						}
						mergeNum = 1;
					}
				}
			}
		} else {
			data[0][name + 'Rowspan'] = 1;
		}
	}
	return data;
}

$("#outputExcel").click(function() {
	aoa = [];
	tempAoa = [];
	var mergeClumn = [];
	var tempObj = {};
	var sData = allData;

	aoa.push(['宝贝姓名', '小名', '出生日期', '父母姓名', '电话', '剩余课时']);

	for(var i = 0; i < sData.length; i++) {
		var thisStudent = sData[i],
			tempArr = [];
		tempAoa.push(thisStudent.name);
		tempAoa.push(thisStudent.nickName);
		tempAoa.push(thisStudent.birthday);
		tempAoa.push(thisStudent.paName);
		tempAoa.push(thisStudent.tel);
		// for(var i = 0; i < thisStudent.buyInfo.length; i++) {
		// 	tempArr.push(thisStudent.buyInfo[i].date || "" + "-" + thisStudent.buyInfo[i].num + "***");
		// }
		// tempAoa.push(tempArr.toString());
		// tempArr = [];
		// if(thisStudent.passClass) {
		// 	for (var i = 0; i < thisStudent.passClass.length; i++) {
		// 		tempArr.push(thisStudent.passClass[i].date + "-");
		// 		tempArr.push(thisStudent.passClass[i].time + "-");
		// 		tempArr.push(thisStudent.passClass[i].classFullName + "-");
		// 		tempArr.push(thisStudent.passClass[i].num + "***");
		// 	}
		// }
		tempAoa.push(thisStudent.classNum.formal + "-" + thisStudent.classNum.free);
		aoa.push(tempAoa);
		tempAoa = [];
	}

	// for (var i = 0; i < sData["info"].length; i++) {
	// 	var classFullName = sData["info"][i].classFullName,
	// 		students = sData["info"][i].students || [],
	// 		teachers = sData["info"][i].teachers || [];
	// 	if(students.length > 0) {
	// 		mergeClumn.push(students.length);
	// 		for (var k = 0; k < students.length; k++) {
	// 			var type = students[k].type;
	// 			tempAoa = [];
	// 			tempAoa.push(sData["date"]);
	// 			tempAoa.push(sData["info"][i].time);
	// 			tempAoa.push(sData["info"][i].classFullName);

	// 			for (var t = 0; t < 2; t++) {
	// 				tempAoa.push(teachers[t] ? teachers[t].name : "");
	// 			}
	// 			tempAoa.push(students[k].name);
	// 			tempAoa.push(students[k].nickName ? students[k].nickName : "");
	// 			tempAoa.push(students[k].allData ? JSON.parse(students[k].allData).birthday : students[k].month);
	// 			tempAoa.push("");//签到
	// 			tempAoa.push(students[k].cc ? students[k].cc : "");//判断第一次状态

	// 			aoa.push(tempAoa);
	// 		}
	// 	}
	// }

	var sheet = XLSX.utils.aoa_to_sheet(aoa);

	// sheet['!merges'] = [];
	// sheet['!merges'].push({s: {r: 1, c: 0}, e: {r: aoa.length - 1, c: 0}});
	// var idx = 1;
	// for (var i = 0; i < mergeClumn.length; i++) {
	// 	sheet['!merges'].push({s: {r: idx, c: 1}, e: {r: idx + mergeClumn[i] - 1, c: 1}});
	// 	sheet['!merges'].push({s: {r: idx, c: 2}, e: {r: idx + mergeClumn[i] - 1, c: 2}});
	// 	sheet['!merges'].push({s: {r: idx, c: 3}, e: {r: idx + mergeClumn[i] - 1, c: 3}});
	// 	sheet['!merges'].push({s: {r: idx, c: 4}, e: {r: idx + mergeClumn[i] - 1, c: 4}});
	// 	idx = idx + mergeClumn[i];
	// }
	// sheet["A2"].s = {vertical:'center', horizontal:'center'};
	openDownloadDialog(sheet2blob(sheet), '会员统计表.xlsx');
});
</script>
</body>
</html>